package com.plusjun.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plusjun.common.utils.PageUtils;
import com.plusjun.common.utils.Query;
import com.plusjun.gulimall.coupon.dao.SeckillSessionDao;
import com.plusjun.gulimall.coupon.entity.SeckillSessionEntity;
import com.plusjun.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.plusjun.gulimall.coupon.service.SeckillSessionService;
import com.plusjun.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Resource
    SeckillSessionDao seckillSessionDao;
    @Resource
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLastest3DaySession() {
        // 计算最近 3 天
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", getStartTime(), getEndTime()));
        if (!CollectionUtils.isEmpty(list)) {
            List<Long> ids = list.stream().map(item -> item.getId()).collect(Collectors.toList());
            List<SeckillSkuRelationEntity> skuRelationEntities = seckillSkuRelationService.listByIds(ids);

            List<SeckillSessionEntity> collect = list.stream().map(item -> {
                Long id = item.getId();
                if (!CollectionUtils.isEmpty(skuRelationEntities)) {
                    List<SeckillSkuRelationEntity> relationEntities = skuRelationEntities.stream().filter(i -> i.getId().equals(id)).collect(Collectors.toList());
                    item.setRelationEntities(relationEntities);
                }
                return item;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;

    }

    private String getStartTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);

        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String getEndTime() {
        LocalDate twoDaysLater = LocalDate.now().plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(twoDaysLater, max);
        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}